using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum SayType
{
    Enemy,Npc
}
public class SayPanel : BasePanel
{
    [Header("说话的数据")]
    public SayData data;
    public int currIndex = 0;

    public Text myNameText;
    public Text sayText;
    public GameObject clickObj;

    public Animator ani;
    //转换时间
    public float changeTimer=1;
    public float changeStopTimer=1;
    //赋值时间
    public float setTimer=0.5f;
    public float setStopTimer=0.5f;

    //只有开局和点击后才可以增加
    public bool canAdd = false;
    public override void OnClose()
    {
        base.OnClose();

        GameManager.Instance.canOprate=true;
    }

    public override void OnInit()
    {
        base.OnInit();
        path="UI/Panel/SayPanel";
        type=PanelType.Panel;

        GameManager.Instance.canOprate=false;
    }

    public override void OnShow(params object[] par)
    {
        base.OnShow(par);

        myNameText=item.transform.Find("Name").GetComponent<Text>();
        sayText=item.transform.Find("Say").GetComponent<Text>();
        clickObj=item.transform.Find("Click").gameObject;
        ani=item.GetComponent<Animator>();

        data=(SayData)(par[0]);
        clickObj.SetActive(false);

        SayOpen();
    }
    private void Update()
    {
        CalChangeTimer();
        CalSetTimer();

        if(clickObj.activeSelf)
        {
            if(Input.GetMouseButtonDown(0))
            {
                if(currIndex+1>=data.data.Count)
                {
                    CloseCheck();
                    return;
                }
                //设置终剑事件
                AboutFinalSwordEvent(currIndex);
                //赋值
                Say();
            }
        }
    }
    //关于终剑话语事件
    public void AboutFinalSwordEvent(int index)
    {
        if(data.myName=="终剑")
        {
            if(index==2)
            {
                //失去超脱石
                GameManager.Instance.ChangeStone(-1);
                //驱除魔气
                data.finalSword.DesMagicEff();
            }
            else if (index==4)
            {
                //拥有剑
                data.finalSword.GetSword();
                AchiManager.instance.UnLockAchi("终剑");
                GameManager.Instance.ChangeFinalSword(1);

                AudManager.instance.PlayBgm(AudManager.instance.bgmDic["拿到终剑"]);
            }
        }
        else if(data.myName=="-终剑-")
        {
            if(index==2)
            {
                //驱除魔气
                data.finalSword.DesMagicEff();
                //拥有剑
                data.finalSword.GetSword();
            }
        }
    }
    //关闭
    public void CloseCheck()
    {
        Close();

        if(data.type==SayType.Enemy)
        {
            if (data.myName=="魔化公主")
            {
                //打开拯救面板
                PanelManager.Open<MagicPrincessTip>(data);

                return;
            }
            if (data.playerHurtObj!=null&&data.player!=null&&data.enemy!=null)
            {
                GameManager.Instance.StartBattle(data.playerHurtObj, data.player, data.enemy);
            }
        }
        else if(data.type==SayType.Npc)
        {
            if(data.myName=="爱心使者")
            {
                if(SceneItemManager.instance.GetNpcData(data.myName))
                {
                    if(data.player.currHp==520)
                    {
                        GameManager.Instance.ChangePlayerHp(500);
                        GameManager.Instance.ChangeDamage(10);
                        GameManager.Instance.ChangeArmor(10);
                        TipSayManager.Instance.Say("生命值<color=yellow> + 500 </color>");
                        TipSayManager.Instance.Say("攻击力<color=yellow> + 10 </color>");
                        TipSayManager.Instance.Say("防御力<color=yellow> + 10 </color>");
                        SceneItemManager.instance.SetNpcFanish(data.myName);

                        AchiManager.instance.UnLockAchi("单身520");
                    }
                    else if (data.player.damage==52&&data.player.armor==52)
                    {
                        GameManager.Instance.ChangeDamage(10);
                        GameManager.Instance.ChangeArmor(10);
                        TipSayManager.Instance.Say("攻击力<color=yellow> + 10 </color>");
                        TipSayManager.Instance.Say("防御力<color=yellow> + 10 </color>");
                        SceneItemManager.instance.SetNpcFanish(data.myName);

                        AchiManager.instance.UnLockAchi("单身520");
                    }
                    else if (data.player.damage==52)
                    {
                        GameManager.Instance.ChangeDamage(5);
                        TipSayManager.Instance.Say("攻击力<color=yellow> + 5 </color>");
                        SceneItemManager.instance.SetNpcFanish(data.myName);

                        AchiManager.instance.UnLockAchi("单身520");
                    }
                    else if (data.player.armor==52)
                    {
                        GameManager.Instance.ChangeArmor(5);
                        TipSayManager.Instance.Say("防御力<color=yellow> + 5 </color>");
                        SceneItemManager.instance.SetNpcFanish(data.myName);

                        AchiManager.instance.UnLockAchi("单身520");
                    }
                    else if (data.player.damage+data.player.armor==52)
                    {
                        GameManager.Instance.ChangeDamage(3);
                        GameManager.Instance.ChangeArmor(3);
                        TipSayManager.Instance.Say("攻击力<color=yellow> + 3 </color>");
                        TipSayManager.Instance.Say("防御力<color=yellow> + 3 </color>");
                        SceneItemManager.instance.SetNpcFanish(data.myName);

                        AchiManager.instance.UnLockAchi("单身520");
                    }
                    else if (Mathf.Abs(data.player.damage-data.player.armor)==52)
                    {
                        GameManager.Instance.ChangeDamage(3);
                        GameManager.Instance.ChangeArmor(3);
                        TipSayManager.Instance.Say("攻击力<color=yellow> + 3 </color>");
                        TipSayManager.Instance.Say("防御力<color=yellow> + 3 </color>");
                        SceneItemManager.instance.SetNpcFanish(data.myName);

                        AchiManager.instance.UnLockAchi("单身520");
                    }
                    //else
                    //{
                    //    GameManager.Instance.ChangeDamage(5);
                    //    GameManager.Instance.ChangeArmor(5);
                    //    TipSayManager.Instance.Say("攻击力<color=yellow> + 5 </color>");
                    //    TipSayManager.Instance.Say("防御力<color=yellow> + 5 </color>");
                    //    SceneItemManager.instance.SetNpcFanish(data.myName);

                    //    AchiManager.instance.UnLockAchi("单身520");
                    //}
                }
            }
            else if(data.myName=="勇者雕像")
            {
                 if(SceneItemManager.instance.GetNpcData(data.myName))
                 {
                    if(GameManager.Instance.circleNum>=10)
                    {
                        GameManager.Instance.ChangeCircle(-10);
                        ArtifactManager.instance.AddArtifact("盾之心");
                        SceneItemManager.instance.SetNpcFanish("勇者雕像");
                    }
                 }
            }
            else if (data.myName == "奸鼠")
            {
                if (SceneItemManager.instance.GetNpcData(data.myName))
                {
                    if (GameManager.Instance.redTurnNum >= 1)
                    {
                        GameManager.Instance.ChangeRedTurn(-1);
                        GameManager.Instance.ChangeKey(1);
                        TipSayManager.Instance.Say("<color=yellow> 生锈的钥匙 + 1 </color>");
                    }
                }
            }
            else if (data.myName == "守塔人*魔")
            {
                //守塔人死亡
                GameManager.Instance.towerMan.GetComponent<TowerMan>().PlayEffect();
            }
            else if(data.myName=="-终剑-")
            {
                DieOver();
            }
            else if(data.myName=="塔灵")
            {
                WinOver();
            }
            else if(data.myName== "终剑")
            {
                //移除npc列表
                SceneItemManager.instance.SetNpcFanish("终剑");
            }
            else if(data.myName=="守塔人*战")
            {
                //开启战斗
                //打开战斗面板，关上门，隐藏其他道具
                GameManager.Instance.towerMan.GetComponent<TowerMan>().StartBattle();
                PanelManager.Open<FinalSwordPanel>("右");
            }
        }
    }
    //初始打开
    public void SayOpen()
    {
        changeTimer=changeStopTimer;
        setTimer=setStopTimer;
        //解析对话
        SetSay(DeCodeSay(data.data[currIndex]));
        //播放动画
        PlayAniOpen();
    }
    //说话
    public void Say()
    {
        changeTimer=changeStopTimer;
        setTimer=setStopTimer;
        //解析对话
        //解析对话
        SetSay(DeCodeSay(data.data[currIndex]));
        //播放动画
        PlayAniRun();
    }
    //切换时间
    public void CalChangeTimer()
    {
        if(changeTimer>0)
        {
            changeTimer-=Time.deltaTime;
            clickObj.SetActive(false);
        }
        else
        {
            clickObj.SetActive(true);
        }
    }
    //赋值时间
    public void CalSetTimer()
    {
        if (setTimer>0)
        {
            setTimer-=Time.deltaTime;
        }
        else
        {
            SetSay(DeCodeSay(data.data[currIndex]));
        }
    }
    //赋值对话
    public void SetSay(string[] str)
    {
        if (canAdd)
        {
            currIndex+=1;
            canAdd=false;
        }
        if (str[0]=="0")
        {
            myNameText.text=data.myName;
        }
        else if(str[0]=="1")
        {
            myNameText.text="玩家";
        }
        else if (str[0]=="2")
        {
            myNameText.text = "系统";
        }
            sayText.text = str[1];
    }
    //SayAni
    public void PlayAniRun()
    {
        ani.SetTrigger("SayRun");
        canAdd=true;
    }
    public void PlayAniOpen()
    {
        ani.SetTrigger("Open");
    }
    //解析对话
    public string[] DeCodeSay(string _str)
    {
        return _str.Split("|");
    }
    //死亡结局
    public void DieOver()
    {
        //死亡结局
        SaveManager.instance.mainData.isDieOver=true;
        //保存数据
        SaveManager.instance.SaveAllData();
        //打开BlackPanel
        PanelManager.Open<BlackPanel>(false, false, 0, false, 2);
    }
    //胜利结局
    public void WinOver()
    {
        //胜利结局
        SaveManager.instance.mainData.isWinOver=true;
        //保存数据
        SaveManager.instance.SaveAllData();
        //打开BlackPanel
        PanelManager.Open<BlackPanel>(false, false, 0, false, 1);
    }
}

//对话
[System.Serializable]
public class SayData
{
    public List<string> data = new List<string>();
    public string myName;
    public SayType type;

    public Enemy enemy;
    public Player player;
    public PlayerHurtObj playerHurtObj;

    public bool haveStone = false;
    //终剑数据
    public FinalSword finalSword;
}